import java.util.*;
import local.util.*;

public class StockRealTimeDataCollecter {
    public static void main( String args[] ) {
	String tick_name = args[0];
	String try_url   = "https://www.bigcharts.com/custom/datek-com/datek-rt2.asp?osymb=&symb="+args[0]+
	    "&time=1dy&freq=1mi&compidx=aaaaa%7E0&comp=&type=2&ma=0&maval=9&uf=0&lf=1&"+
	    "lf2=0&lf3=0&sid=&x=42&y=6";
  
	StockCollectData cl = new StockCollectData();
	cl.setTickName(tick_name);
	cl.setUrl(try_url);
	StockDataCollecter thrd = new StockDataCollecter(cl);
	thrd.start();
    }
}

class StockDataCollecter extends Thread {
    private StockCollectData cldata = null;
    private int sleepTime = 10000*6*5; // 10 seconds * 6 * 5

    public void setSleepTime (int sleep_time) {
	sleepTime = sleep_time;
    }

    public StockDataCollecter( StockCollectData cd ) {
	super( "StockDataCollect" );
	cldata = cd;
    }

   public void run()
   {
       for (int i=0; i<10; i++) {
	   if (cldata != null) {
	       System.out.println("Runing data collection...");
	       cldata.loadData();
	   }
	   
	   try {
	       Thread.sleep(sleepTime);
	   }
	   catch ( InterruptedException exception ) {
	       System.err.println( exception.toString() );
	   }
       }
   }
}

class StockCollectData {

    static String last_volume = "0";
    static String symbol = "";
    String data_url = null;

    public void setTickName(String tick) {
	symbol = tick;
    }

    public void setUrl(String url) {
	data_url = url;
    }

    public void loadData () {
	Hashtable dHashTable = new Hashtable();
	int time_h =0;
	int time_m =0;
	String this_volume = "0";
	String query_str ="";
	Calendar c = Calendar.getInstance();

	GetTodaysData dc = new GetTodaysData();
	dc.setUrl(symbol);
	dc.setUrl(data_url);     
	dc.loadData();
	dHashTable = dc.getTodaysDataHashTable(symbol);
	this_volume = ((String)dHashTable.get("Volume")).replaceAll(",","");

	if (c.get(Calendar.AM_PM)==1) { //PM
	    time_h = c.get(Calendar.HOUR)+12;
	}
	else {
	    time_h = c.get(Calendar.HOUR);
	}

	time_m = c.get(Calendar.MINUTE);

	if(!(this_volume.equals(last_volume))) {
	    last_volume = ((String)dHashTable.get("Volume")).replaceAll(",","");
	}
    }
}


